<template>
  <div class="flex flex-col h-full">
    <SearchContainer @search="getData" @reset="onReset">
      <a-form ref="formRef" class="form-style-flex" :model="formState" :label-col="{ style: { width: '70px' } }">
        <a-form-item name="dateRange" label="手术日期" class="w-72 mr-2">
          <a-range-picker v-model:value="formState.dateRange" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
        </a-form-item>
        <a-form-item name="createDateRange" label="创建日期" class="w-72 mr-2">
          <a-range-picker v-model:value="formState.createDateRange" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
        </a-form-item>
        <a-form-item name="zjm" class="w-64 mr-2">
          <a-input v-model:value="formState.zjm" placeholder="姓名/助记码/卡号/手机号/身份证号" />
        </a-form-item>
        <a-form-item label="手术名称" class="w-64 mr-2" name="ssdm">
          <!-- <a-select
            v-model:value="formState.ssdm"
            placeholder="请选择"
            show-search
            allow-clear
            :options="surgeryOptionList"
            :filter-option="false"
            @search="getSurgery"
          /> -->
          <a-input v-model:value="formState.ssdm" placeholder="请输入手术名称" />
        </a-form-item>
        <a-form-item label="手术医生" class="w-64" name="aptmOprEmpId">
          <SelectDoctor v-model:value="formState.aptmOprEmpId" :search-type="true" />
        </a-form-item>
        <a-form-item label="业务员" class="w-64">
          <SelectTable v-model:value="formState.brYwyName" type="员工" allow-clear page-size="999" @select="onSelectEmployee('brYwy', $event)" />
        </a-form-item>
        <a-form-item label="咨询师" class="w-64" name="brZxs">
          <a-select
            v-model:value="formState.brZxs"
            placeholder="请选择"
            show-search
            allow-clear
            :filter-option="false"
            :options="Counselors"
            @search="getCounselors"
          />
        </a-form-item>
        <a-form-item label="负责人" class="w-64">
          <SelectTable v-model:value="formState.brFzrName" type="员工" allow-clear page-size="999" @select="onSelectEmployee('brFzr', $event)" />
        </a-form-item>
        <a-form-item label="显示作废手术" class="w-64 ml-4" :label-col="{ style: { width: '85px' } }">
          <a-switch v-model:checked="formState.statusList" class="ml-2"
        /></a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2 flex-1">
      <div>
        <a-button type="primary" class="mb-2 mr-2" @click="onAdd()"> 新增 </a-button>
        <BaseExport type="手术" name="手术预约" code="000036" :search-params="{ ...formState, statusList: formState.statusList ? [0, 1, 2, 3] : [0, 1, 2] }"
          >导出</BaseExport
        >
      </div>
      <BaseTable :loading="loading" column-code="000036" fill :scroll="{ x: 2100 }" :data-source="tableData" bordered :pagination="pagination">
        <template #bodyCell="{ column, record }">
          <template v-if="column.dataIndex == 'statusStr'">
            <a-tag v-if="record.status === '0'" color="blue">未登记</a-tag>
            <a-tag v-else-if="record.status === '1'" color="green">已登记</a-tag>
            <a-tag v-else-if="record.status === '2'" color="green">已手术</a-tag>
            <a-tag v-else color="red">已作废</a-tag>
          </template>
          <template v-if="column.dataIndex == 'brYwyName'">
            {{ record?.brYwyName || record?.brYwy }}
          </template>
          <template v-if="column.dataIndex === 'action'">
            <a-button type="link" size="small" :disabled="record.status !== '0'" @click="onEdit(record)"> 修改 </a-button>
            <a-divider type="vertical" />
            <template v-if="record.status === '0'">
              <a-button type="link" size="small" :disabled="record.destOrgCode && record.destOrgCode !== ORG_CODE" @click="onRegister(record)"> 登记 </a-button>
              <a-divider type="vertical" />
            </template>
            <BaseButton confirm-text="是否作废?" :disabled="record.status !== '0'" type="text" danger size="small" @click="onCancel(record)"> 作废 </BaseButton>
          </template>
        </template>
      </BaseTable>
    </a-card>
    <Edit v-model:open="editOpen" :row-data="rowData" @success="onSuceess" />
    <Register v-model:open="registerOpen" :row-data="rowData" @success="onSuceess" />
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import dayjs from 'dayjs'
import { message } from 'ant-design-vue'
import SearchContainer from '@/components/SearchContainer'
import { post } from '@/utils/request'
import appStore from '@/store/app'
import Register from '../components/Register.vue'
import Edit from './Edit'

const { ORG_CODE } = appStore()
const surgeryOptionList = ref([])
const rowData = ref({})
const editOpen = ref(false)
const registerOpen = ref(false)
const formRef = ref(null)
const formState = ref({
  dateRange: [dayjs().format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')],
  // createDateRange: [dayjs().format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')],
  createDateRange: [],
  status: '',
  statusList: false
})
const Counselors = ref([])

const onSelectEmployee = (key, select) => {
  formState.value[key + 'Name'] = select.empName
  formState.value[key] = select.empId
}

const onRegister = (row) => {
  rowData.value = { ...row, isRegister: true }
  registerOpen.value = true
}

const onEdit = async (row) => {
  rowData.value = row
  editOpen.value = true
}
const onAdd = () => {
  rowData.value = {}
  editOpen.value = true
}
const getCounselors = async (mcbm = '') => {
  const { code, data } = await post('/operation/opex/getEmpInfoList', {
    yxzt: 0,
    mcbm,
    emplbid: '14',
    pageNum: 1,
    pageSize: 9999
  })
  if (code !== 0) return
  Counselors.value = data.map((item) => {
    return {
      label: item.empName,
      value: item.empId
    }
  })
}
//手术检索

const getSurgery = async () => {
  const { code, data } = await post('/common/masterData/getCanUseDrugList', {
    xmlbList: [6]
  })
  if (code !== 0) return
  surgeryOptionList.value = data.map((item) => {
    return {
      label: item.mc,
      value: item.bh
    }
  })
}
const { loading, tableData, pagination, getData } = useTable({
  url: '/operation/opex/getAppointmentRecordList',
  immediate: true,
  params: formState.value,
  formatParams: (params) => {
    return {
      ...params,
      stdate: params.dateRange?.[0],
      eddate: params.dateRange?.[1],
      createTimeStdate: params.createDateRange?.[0],
      createTimeEddate: params.createDateRange?.[1],
      statusList: params.statusList ? [0, 1, 2, 3] : [0, 1, 2]
    }
  }
})
const onCancel = async (record) => {
  const res = await post('/operation/opex/doUpdateAppointmentStatus', record)
  if (res.code !== 0) return message.error(res.msg)
  getData()
}

const onSuceess = () => {
  editOpen.value = false
  rowData.value = {}
  getData()
}

const onReset = () => {
  formRef.value.resetFields()
  formState.value.brYwyName = ''
  formState.value.brYwy = ''
  formState.value.brFzrName = ''
  formState.value.brFzr = ''
  formState.value.statusList = false
}

onMounted(() => {
  getSurgery()
  getCounselors()
})
</script>
